import java.awt.Color;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;

/**
 * @author Zero
 */
public class Frame extends JFrame {

	private static final long serialVersionUID = -7433048974374459563L;
	
	private Panel contentPane;
    private JPanel panelMain;

    public Frame() {
        setUndecorated(true);
        setBounds(100, 100, 560, 386);
        setBackground(new Color(0, 0, 0, 0));

        // 拖拽处理
        // 匿名内部类只能使用外部final类型的变量
        final Point[] p = new Point[1];
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                p[0] = e.getPoint();
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                Point p1 = e.getPoint();
                Point p2 = getLocation();
                p2.x += p1.x - p[0].x;
                p2.y += p1.y - p[0].y;
                setLocation(p2);
            }
        });

//        //拖拽处理和改变大小因为事件冲突只能实现一种
//        // 监听鼠标事件以改变大小
//        addMouseMotionListener(new MouseMotionAdapter() {
//            // 鼠标指针位置标识
//            private boolean up = false;
//            private boolean down = false;
//            private boolean left = false;
//            private boolean right = false;
//
//            @Override
//            public void mouseMoved(MouseEvent e) {
//                // 鼠标指针位置判断
//                // 我不知道为什么要减2，我只能说实践出真知（猜测可能是边框原因，但我把边框调大也是如此）
//                if (e.getPoint().getY() <= 1) {
//                    setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
//                    up = true;
//                } else if (getHeight() - e.getPoint().getY() - 2 <= 1) {
//                    setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
//                    down = true;
//                } else if (e.getPoint().getX() <= 1) {
//                    setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
//                    left = true;
//                } else if (getWidth() - e.getPoint().getX() - 2 <= 1) {
//                    setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
//                    right = true;
//                } else {
//                    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
//                    up = false;
//                    down = false;
//                    left = false;
//                    right = false;
//                }
//            }
//
//            @Override
//            public void mouseDragged(MouseEvent e) {
//                Dimension dimension = getSize();
//                if (up) {
//                    dimension.setSize(dimension.getWidth(), dimension.getHeight() - e.getY());
//                    setLocation(getLocationOnScreen().x, getLocationOnScreen().y + e.getY());
//                    setSize(dimension);
//                } else if (down) {
//                    dimension.setSize(dimension.getWidth(), e.getY());
//                    setSize(dimension);
//                } else if (left) {
//                    dimension.setSize(dimension.getWidth() - e.getX(), dimension.getHeight());
//                    setLocation(getLocationOnScreen().x + e.getX(), getLocationOnScreen().y);
//                    setSize(dimension);
//                } else if (right) {
//                    dimension.setSize(e.getX(), dimension.getHeight());
//                    setSize(dimension);
//                }
//            }
//        });

        contentPane = new Panel();
        setContentPane(contentPane);

        JButton btnMax = new JButton("MAX");
        btnMax.setUI(new ButtonUI());
        btnMax.setBorderPainted(false);
        btnMax.setOpaque(false);
        btnMax.setForeground(Color.white);
        btnMax.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnMax.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {
                    setExtendedState(JFrame.NORMAL);
                } else {
                    setExtendedState(JFrame.MAXIMIZED_BOTH);
                }
            }
        });

        JButton btnMix = new JButton("MIX");
        btnMix.setUI(new ButtonUI());
        btnMix.setBorderPainted(false);
        btnMix.setOpaque(false);
        btnMix.setForeground(Color.white);
        btnMix.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnMix.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                setExtendedState(JFrame.ICONIFIED);
            }
        });

        JButton btnExit = new JButton("EXIT");
        btnExit.setUI(new ButtonUI());
        btnExit.setBorderPainted(false);
        btnExit.setOpaque(false);
        btnExit.setForeground(Color.white);
        btnExit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnExit.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }
        });

        panelMain = new JPanel();
        panelMain.setOpaque(false);

        GroupLayout glPanelMain = new GroupLayout(panelMain);
        glPanelMain.setHorizontalGroup(glPanelMain.createParallelGroup(GroupLayout.Alignment.TRAILING).addGap(0, 540, Short.MAX_VALUE));
        glPanelMain.setVerticalGroup(glPanelMain.createParallelGroup(GroupLayout.Alignment.TRAILING).addGap(0, 334, Short.MAX_VALUE));
        panelMain.setLayout(glPanelMain);

        GroupLayout glContentPane = new GroupLayout(contentPane);
        glContentPane.setHorizontalGroup(glContentPane.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, glContentPane.createSequentialGroup().addContainerGap(313, Short.MAX_VALUE).addComponent(btnMax, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(btnMix, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(btnExit, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)).addComponent(panelMain, GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE));
        glContentPane.setVerticalGroup(glContentPane.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(glContentPane.createSequentialGroup().addGroup(glContentPane.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(btnExit).addComponent(btnMix).addComponent(btnMax)).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(panelMain, GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)));
        contentPane.setLayout(glContentPane);
    }
}